What is range-parser?
The range-parser package is a utility for parsing HTTP range header fields. It's useful for handling requests for specific ranges of data, which is particularly relevant for serving large files in smaller chunks, such as videos or large documents.
What are range-parser's main functionalities?
Parse Range Header
This feature allows you to parse the 'Range' header from HTTP requests. It takes the size of the file and the range header as arguments and returns an array of ranges, or an error code if the range is unsatisfiable or syntactically invalid.
const rangeParser = require('range-parser');
const rangeHeader = 'bytes=0-499';
const fileSize = 1000;
const parsedRanges = rangeParser(fileSize, rangeHeader);
Other packages similar to range-parser
http-range
The http-range package is similar to range-parser in that it also parses HTTP range headers. However, it provides a more high-level API and includes additional features for creating and manipulating ranges.
accept-ranges
The accept-ranges package is designed to parse the Accept-Ranges header in HTTP. While it serves a different purpose from range-parser, it is related in the context of HTTP range requests and responses.
range-parser
Range header field parser.
Installation
This is a Node.js module available through the
npm registry. Installation is done using the
npm install
command:
$ npm install range-parser
API
var parseRange = require('range-parser')
Parse the given header
string where size
is the maximum size of the resource.
An array of ranges will be returned or negative numbers indicating an error parsing.
-2
signals a malformed header string-1
signals an unsatisfiable range
var range = parseRange(size, req.headers.range)
if (range.type === 'bytes') {
range.forEach(function (r) {
})
}
Options
These properties are accepted in the options object.
combine
Specifies if overlapping & adjacent ranges should be combined, defaults to false
.
When true
, ranges will be combined and returned as if they were specified that
way in the header.
parseRange(100, 'bytes=50-55,0-10,5-10,56-60', { combine: true })
License
MIT